<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page import="java.util.List" %>
<%@ page import="java.io.File" %>
<%@ page import="java.util.UUID" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--<%
    request.setCharacterEncoding("utf-8");
    String name=request.getParameter("username");
    String idcard=request.getParameter("idcardpic");
%>
<h1>姓名：<%=name%>,身份证:<%=idcard%></h1>
<hr/>
<%
    java.io.InputStream in=request.getInputStream();
    int ch=0;
    while((ch=in.read())!=-1){
        out.print((char)ch);
    }
%>--%>
<%
    DiskFileItemFactory factory = new DiskFileItemFactory();
    // 内存存储的最大值
    factory.setSizeThreshold(4096);
    ServletFileUpload upload=new ServletFileUpload(factory);
    upload.setHeaderEncoding("utf-8");
    List<FileItem> list=upload.parseRequest(request);
    for(FileItem f:list){


%>
<p>
    <%
        if(f.isFormField()) {
            out.print("普通元素,元素名" + f.getFieldName()+","+f.getString("utf-8"));
        }else{
            out.print("文件元素,元素名" + f.getFieldName()+",文件名:"+f.getName());
            File path=new File(application.getRealPath("img"));
            if(path.exists()==false)
                path.mkdirs();
            String oldName=f.getName();
            //改名的时候保留后缀
            String houzhui=oldName.substring(oldName.lastIndexOf('.'));
            String newName= UUID.randomUUID()+houzhui;
            f.write(new java.io.File(path,newName));
        }
    %>
</p>
<%}%>
</body>
</html>
